<template>
  <div class="main">
    <section>
      <el-button type="primary" style="margin-bottom:20px" @click="addKeyWord">新建关键词</el-button>
      <el-table v-loading="loading" :data="list" element-loading-text="给我一点时间" border fit highlight-current-row>
        <el-table-column align="center" prop="id" label="ID">
        </el-table-column>
        <el-table-column align="center" prop="keyword" label="关键字文本">
        </el-table-column>
        <el-table-column align="center" prop="updatedAt" label="修改时间">
        </el-table-column>
        <el-table-column align="center" label="操作" width="210">
          <template slot-scope="scope">
            <el-button type="primary" plain @click="editKeyword(scope.row)">编辑</el-button>
            <el-button type="danger" plain @click="deleteComfir(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page.sync="listQuery.pageNum"
        :page-sizes="[10, 20, 50, 100]" :page-size="listQuery.pageSize" background layout="total, sizes, prev, pager, next, jumper"
        :total="listTotal"></el-pagination>
    </section>
    <el-dialog title="搜索关键字" :visible.sync="dialogVisible" width="30%">
      <div style="display:flex;align-items:center">
        <span>搜索关键字:</span>
        <el-input style="width:200px" v-model="currentKeyword.keyword" />
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import {
  getKeyWord,
  deleteKeyWord,
  addKeyWord,
  updateKeyWord
} from '@/api/bihuasuan/keyword'
import moment from 'moment'
export default {
  name: 'b-keyword',

  data() {
    return {
      listQuery: {
        pageNum: 0,
        pageSize: 10
      },
      dialogVisible: false,
      loading: true,
      listTotal: 0,
      list: [],
      currentKeyword: {},
      type: 0 // 0 新建 1 编辑
    }
  },
  created() {
    this.getList()
  },
  methods: {
    async getList() {
      this.loading = true
      try {
        const { data } = await getKeyWord(this.listQuery)
        this.loading = false
        this.list = data.list
        this.listTotal = data.total
      } catch (error) {
        console.log(error)
        this.list = []
        this.listTotal = 0
        this.loading = false
      }
    },
    deleteComfir(val) {
      this.$confirm('是否删除当前关键字?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this._deleteKeyword(val)
        })
        .catch(error => {
          console.log('sss', error)
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    async _deleteKeyword(val) {
      try {
        const { data } = await deleteKeyWord(val)
        this.$message.success('操作成功')
        this.getList()
      } catch (error) {
        console.log(error)
      }
    },

    addKeyWord() {
      this.currentKeyword = { keyword: '' }
      this.dialogVisible = true
      this.type = 0
    },
    editKeyword(val) {
      this.currentKeyword = { ...val }
      this.dialogVisible = true
      this.type = 1
    },
    async submit() {
      try {
        let data = null
        if (this.type === 0) {
          data = await addKeyWord(this.currentKeyword)
        } else {
          data = await updateKeyWord(this.currentKeyword)
        }
        this.$message.success('操作成功')
        this.dialogVisible = false
        this.getList()
      } catch (error) {
        console.log(error)
      }
    },
    search() {
      this.getList()
    },
    //切换每页条数
    handleSizeChange(value) {
      this.listQuery.pageSize = value
      this.getList()
    },
    // 切换分页
    handleCurrentChange(page) {
      this.listQuery.pageNum = page
      this.getList()
    }
  }
}
</script>

 